//package com.example.demo.util;
//
//import com.example.demo.base.ElasticsearchBasePo;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.util.BeanUtil;
//import org.apache.http.HttpHost;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.get.GetResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * ElasticsearchUtil 搜索引擎工具类
// *
// * @author 陈学礼
// * @date 2020/6/3 11:45
// * 你是一段成熟的代码，要学会自己改bug
// */
//public class ElasticsearchUtil {
//
//    public static void main(String[] args) throws Exception {
//        ElasticsearchBasePo po = new ElasticsearchBasePo();
//        po.setId("111");
//        Map<String, Object> map = new HashMap<>();
//        map.put("test", "11");
//        po.setSource(map);
//        po.setIndex("test1");
//        po.setType("type1");
//        ElasticsearchUtil.getInstance().insertOrUpdate(po);
//    }
//
//    private static volatile ElasticsearchUtil INSTANCE;
//    private ElasticsearchUtil() {}
//
//    public static ElasticsearchUtil getInstance() {
//        synchronized (ElasticsearchUtil.class){
//            if (INSTANCE == null) {
//                INSTANCE = new ElasticsearchUtil();
//            }
//            return INSTANCE;
//        }
//    }
//
//    RestHighLevelClient client = null;
//
//    static {
//        // 初始化
//        getInstance().init();
//    }
//
//    /**
//     * 初始化
//     */
//    private void init () {
//        String[] ips = {"127.0.0.1:9200"};
//        HttpHost[] httpHosts = new HttpHost[ips.length];
//        for (int i = 0; i < ips.length; i++) {
//            httpHosts[i] = HttpHost.create(ips[i]);
//        }
//        RestClientBuilder builder = RestClient.builder(httpHosts);
////        RestHighLevelClient client = new RestHighLevelClient(builder);
//        client = new RestHighLevelClient(builder);
//    }
//
//    /**
//     * 插入更新数据
//     * @param po
//     * @throws Exception
//     */
//    public void insertOrUpdate(ElasticsearchBasePo po) throws Exception {
//        IndexResponse response = client.pre
//        IndexRequest request = new IndexRequest(po.getIndex(), po.getType(), po.getId());
//        request.source(po.getSource());
//        client.index(request);
//    }
//
//    /**
//     * 删除数据
//     * @param po
//     * @throws Exception
//     */
//    public void delete(ElasticsearchBasePo po) throws Exception {
//        DeleteRequest request = new DeleteRequest(po.getIndex(), po.getType(), po.getId());
//        client.delete(request);
//    }
//
//    /**
//     * 获取
//     * @param po
//     * @param clazz
//     * @param <T>
//     * @return
//     * @throws Exception
//     */
//    public <T> T get(ElasticsearchBasePo po, Class<T> clazz) throws Exception {
//        GetRequest request = new GetRequest(po.getIndex(), po.getType(), po.getId());
//        GetResponse response = client.get(request);
//        String json = response.getSourceAsString();
//        ObjectMapper mapper = new ObjectMapper();
//        return mapper.readValue(json, clazz);
//    }
//
//}
